package il.ac.technion.wiki.impl.tests;

import static org.junit.Assert.*;
import il.ac.technion.wiki.api.IWikiEngine.IWikiContent;
import il.ac.technion.wiki.api.IDataStore.DataStoreException;
import il.ac.technion.wiki.impl.WikiEngine;
import il.ac.technion.wiki.impl.WikiEngine.WikiContent;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestWikiContent {
	static WikiEngine wiki;
	static DatastoreStub ds;
	WikiContent wc;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ds = new DatastoreStub();
		wiki = new WikiEngine(ds, null, null);
		ds.setContentClass(WikiEngine.WikiContent.class);
		ds.setWikiEngine(wiki);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}
	
	@Before
	public void setUp() throws Exception {
		wc = wiki.new WikiContent();
		wc.setName("foo");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSetText() {
		wc.setText("ThisIsText");
		assertEquals("ThisIsText", wc.getText());
	}

	@Test
	public void testSetMetadata() {
		wc.setMetadata(IWikiContent.MetadataType.AUTHOR, "Slava");
		assertEquals("Slava", wc.getMetadata(IWikiContent.MetadataType.AUTHOR));
		wc.setMetadata(IWikiContent.MetadataType.DATE, "1984");
		assertEquals("1984", wc.getMetadata(IWikiContent.MetadataType.DATE));
		wc.setMetadata(IWikiContent.MetadataType.LANGUAGE, "Gibberish");
		assertEquals("Gibberish", wc.getMetadata(IWikiContent.MetadataType.LANGUAGE));
	}

	@Test
	public void testSetRevision() {
		int rev;
		for (rev = 0; rev < 50; rev++) {
			assertEquals(rev, wc.getRevision());
			try {
				wc.setText(wc.getText());	// just so there's a "change"
				wc.save();
			} catch (DataStoreException e) {
				fail();
			}
		}
		
		wc.apply();
		try {
			wc.save();
		} catch (DataStoreException e) {
			fail();
		}
		assertEquals(rev, wc.getRevision());
		wc.setRevision(5);
		assertEquals(5, wc.getRevision());
	}

	@Test
	public void testRollBack() {
		try {
			wc.rollBack(5000);
		} catch (DataStoreException e) {
			fail();
		}
		assertEquals(5000, wc.getRevision());
		assertEquals("getted", wc.getName());
		assertNotNull(wc.getMetadata(IWikiContent.MetadataType.DATE));
		try {
			wc.save();
		} catch (DataStoreException e) {
			fail();
		}
		assertEquals(5000, wc.getRevision());	// make sure saving didn't advance the revision, since this is a rollback!
		wc.setText(wc.getText());	// just so there's a "change"
		try {
			wc.save();
		} catch (DataStoreException e) {
			fail();
		}
		assertEquals(5001, wc.getRevision());	// no longer a rollback!
	}

	@Test
	public void testSetName() {
		wc.setName("aha");
		assertEquals("aha", wc.getName());
	}

	@Test
	public void testSave() {
		try {
			wc.save();
		} catch (DataStoreException e) {
			fail();
		}
		assertEquals("put", ds.last_action);
	}

	@Test
	public void testDelete() {
		try {
			wc.delete();
		} catch (DataStoreException e) {
			fail();
		}
		assertEquals("delete", ds.last_action);
		assertEquals(null, wc.getText());
	}

}
